<template>
  <div class="login-container">
    <div class="bgimg">
      <!-- <img class="top-logo" src="@/assets/common/m6Logo.png" alt=""> -->
    </div>
    <div class="formclass">
      <el-form ref="loginForm" :model="loginForm" size="mini" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <div class="title-container">
          <!-- <h3 class="title">hamco数字化</h3> -->
          <img class="top-logo" src="@/assets/common/96377.png" alt="">
        </div>
        <!-- 用户名 -->
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input ref="username" v-model.trim="loginForm.username" placeholder="Username" name="username" type="text" tabindex="1" auto-complete="on" />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model.trim="loginForm.password"
            :type="passwordType"
            placeholder="Password"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">Login</el-button>
      </el-form>
    </div>
    <div class="footer-box">
      <ul class="footer-ul">
        <!-- <li><a href="http://www.hamco.net.cn/">{{ $l("公司官网") }}</a><span style="margin-left:30px;margin-right:30px">|</span> </li>
        <li>邮箱：hamco@hamco.net.cn <span style="margin-left:30px;margin-right:30px">|</span> </li>
        <li>公司地址：江苏省太仓市经济开发区浏阳路116号 <span style="margin-left:30px;margin-right:30px">|</span> </li>
        <li>服务热线：0512-53598031</li> -->
      </ul>
    </div>
  </div>
</template>

<script>
import { validMobile, validMobile2, validMobile3 } from '@/utils/validate'
import { mapActions } from 'vuex'
import { Message } from 'element-ui'
import { monitorfn } from '@/utils/monitor'
import { localgetuserNo } from '@/utils/auth'
// import request from '@/utils/request'
// import { reqlogin } from '@/api/user'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validMobile(value) && !validMobile3(value)) {
        callback(new Error('请输入正确的用户名（字母和数字）'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能小于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: JSON.parse(localgetuserNo()) || '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '不能为空', trigger: ['blur', 'change'] },
          { validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },

  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },

  methods: {
    ...mapActions('user', ['login']),
    // 密码的是否可读
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 登录
    async handleLogin() {
      this.$refs.loginForm.validate((vaild) => {
        // 校验不通过
        if (!vaild) return Message.error('账号或密码格式错误')
        this.login(this.loginForm)
          .then((res) => {
            // 路由跳转 不能带数据 因为是异步所以不能直接跳转
            Message.success('登录成功')
            this.$router.push('/')
            // monitorfn(this)
            // sessionStorage.setItem('ismute', true)
            location.reload()
            // 可以带数据
            // this.$router.path({
            //   push: '/'
            // })
          })
          .finally(() => {
            // 这个函数会在promise成功或者失败以后 都会调用
            this.loading = false
          })
        // .catch((error) => {
        //   console.log(error)
        //   Message.error('登录失败')
        // })
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: black !important;
  }
}
.bgimg {
  width: 100%;
  height: 25vh;
  // background-image: url("~@/assets/common/top-bg.png");
  background-size: 100% 100%;
}
.top-logo {
  width: 150px;
  margin-left: 50%;
  margin-top: 40px;
  transform: translate(-50%);
}
.formclass {
  margin-top: 50px;
  width: 600px;
  margin-left: 75%;
  transform: translate(-50%);
  background-color: rgba(254, 254, 254, 0.5);
  border-radius: 10px;
  box-shadow: 15px 17px 40px 2px #e6edf7;
}
.footer-box {
  width: 100%;
  height: 10vh;
  //background-color: #007ddd;
  position: fixed;
  bottom: 0;
}
.footer-ul {
  list-style-type: none;
  padding-left: 20%;
  color: white;
  line-height: 7vh;
  font-size: 1vw;
}
.footer-ul li {
  float: left;
}
/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #eee;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: #fff;
  background-image: url("~@/assets/common/bg.jpg");
  opacity: 0.8;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 25px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: black;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
